#include "TopSyncFunctions.h"
#include "ProtocolSerializer.h"
#include "Helpers.h"



unsigned char mainBuff[200];

void InitialiseTopSyncMessageHeader(ToPSyncMsgHeader* header,
                                    ToPSyncMsgSet messageSetId,
                                    ToPSyncMsgLength messageLength,
                       ToPSyncMsgType messageType,
                                    ToPSyncMsgSequenceNumber sequenceNumber,
                                    ToPSyncMsgSequenceNumber originatingSequenceNumber,
                                    Bool endOfMessage,
                                    ToPSyncConfigResult result,
                                    uint32 securityKey)
{
	header->messageSetId = messageSetId;
	header->messageLength = messageLength;
	header->messageType = messageType;
	header->sequenceNumber = sequenceNumber;
	header->originatingSequenceNumber = originatingSequenceNumber;
	header->endOfMessage = endOfMessage;
	header->result = result;
  header->securityKey = securityKey;

#ifdef __SERIALIZER__
  SerializeToPSyncMsgHeader(header);
#endif

}



void WriteInteger(uint8** dataPtr, uint32 value, uint8 bytes)
{
	uint8* bytePtr;
	uint32 swappedValue;

	/* swap byte order */
	swappedValue = hostToNetworkLong(value);
	bytePtr = (uint8*)(&swappedValue);
	bytePtr += (sizeof(swappedValue) - bytes);
	memcpy(*dataPtr, bytePtr, bytes);
	*dataPtr += bytes;
}


void WriteBoolean(uint8** dataPtr, Bool value)
{
	WriteInteger(dataPtr,value? 0x1 : 0,1);
}

void WriteToPSyncMessageHeader(uint8** data, const ToPSyncMsgHeader* header)
{
#ifdef __SERIALIZER__
  uint8* tmp = *data;
#endif
	WriteInteger(data, (uint32)header->messageSetId, 2);
	WriteInteger(data, (uint32)header->messageLength, 1);
	WriteInteger(data, (uint32)header->messageType, 2);
	WriteInteger(data, (uint32)header->sequenceNumber, 2);
	WriteInteger(data, (uint32)header->originatingSequenceNumber, 2);
	WriteBoolean(data, header->endOfMessage);
	WriteInteger(data, (uint32)header->result, 1);
  WriteInteger(data, (uint32)header->securityKey, 4);

#ifdef __SERIALIZER__
  SerializeToPSyncMsgHeaderBinData((const uint8*)tmp, sizeof(ToPSyncMsgHeader));
#endif
}

